package com.smartleanx.module.hrm.service.esign.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartleanx.module.hrm.dal.dataobject.esign.HrmETempDept;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEFolderMapper;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.service.esign.service.HrmEFolderService;
import com.smartleanx.module.hrm.service.esign.service.HrmEFolderTempService;
import com.smartleanx.module.hrm.service.esign.service.HrmETempDeptService;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolder;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolderTemp;
import com.smartleanx.module.hrm.service.esign.temp.result.HrmEFolderResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * e签宝文件夹Service实现
 *
 * @author xzq
 * @since 2025-07-07 16:43:53
 */
@Service
public class HrmEFolderServiceImpl extends ServiceImpl<HrmEFolderMapper, HrmEFolder> implements HrmEFolderService {

    @Autowired
    private HrmEFolderTempService hrmEFolderTempService;
    @Autowired
    private HrmETempDeptService hrmETempDeptService;

    @Override
    public boolean removeFolder(HrmEFolder hrmEFolder) {
        HrmEFolder hrmEFolder1 = getById(hrmEFolder.getId());
        if(ObjUtil.isEmpty(hrmEFolder1)){
            throw exception(ErrorCodeConstants.FOLDER_IS_NULL_ERROR);
        }

        List<HrmEFolderTemp> folderTempList = hrmEFolderTempService.getFolderListByFolderId(hrmEFolder.getId());
        if(CollUtil.isNotEmpty(folderTempList)){
            throw exception(ErrorCodeConstants.E_TEMP_FOLDER_IS_NOT_NULL);
        }

        hrmEFolder1.setDataStatus(0);

        updateById(hrmEFolder1);
        return Boolean.TRUE;
    }

    @Override
    public HrmEFolder getFolderDetail(Long id) {
        HrmEFolder hrmEFolder = getById(id);
        if(ObjUtil.isEmpty(hrmEFolder)){
            throw  exception(ErrorCodeConstants.FOLDER_DETAIL_IS_NULL);
        }
        return hrmEFolder;
    }

    @Override
    public List<HrmEFolderResult> getFolderResult(String type) {
//        List<HrmETempDept> tempDeptList = hrmETempDeptService.list();
//        if(CollUtil.isEmpty(tempDeptList)){
//            return new ArrayList<>();
//        }
//        List<Long> tempIds = tempDeptList.stream().map(HrmETempDept::getTempId).distinct().collect(Collectors.toList());
//        List<HrmEFolderTemp> folderTempList = hrmEFolderTempService.list(new LambdaQueryWrapper<HrmEFolderTemp>()
//                .eq(HrmEFolderTemp::getDataStatus, 1)
//                .in(HrmEFolderTemp::getTempId, tempIds));
//        List<Long> folderIds = folderTempList.stream().map(HrmEFolderTemp::getFolderId).distinct().collect(Collectors.toList());

        List<HrmEFolder> list = list(new LambdaQueryWrapper<HrmEFolder>()
                .eq(HrmEFolder::getDataStatus, 1)
                .eq(HrmEFolder::getFolderType,type)
//                .in(HrmEFolder::getId,folderIds)
        .orderByAsc(HrmEFolder::getSortNo));

        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }

        Map<Long, List<HrmEFolder>> map = list.stream().collect(Collectors.groupingBy(HrmEFolder::getParentId));
        List<HrmEFolder> hrmEFolders = map.get(0L);
        List<HrmEFolderResult> hrmEFolderResults = new ArrayList<>();
        if(CollUtil.isEmpty(hrmEFolders)){
            return hrmEFolderResults;
        }
        getFolder(hrmEFolders,hrmEFolderResults,map);
        return hrmEFolderResults;
    }

    @Override
    public List<HrmEFolderResult> getParentFolderResult(String type) {
        List<HrmEFolder> list = list(new LambdaQueryWrapper<HrmEFolder>()
                .eq(HrmEFolder::getDataStatus, 1)
                .eq(HrmEFolder::getFolderType,type));
        if(CollUtil.isEmpty(list)){
            return new ArrayList<>();
        }
        List<HrmEFolderResult> collect = list.stream().map(p -> {
            HrmEFolderResult hrmEFolderResult = new HrmEFolderResult();
            BeanUtil.copyProperties(p, hrmEFolderResult);
            return hrmEFolderResult;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<HrmEFolder> getFolderByIds(List<Long> ids) {
        return list(new LambdaQueryWrapper<HrmEFolder>()
        .eq(HrmEFolder::getDataStatus,1).in(HrmEFolder::getId,ids));
    }

    @Override
    public List<HrmEFolder> getFolderListByFolderId(Long id) {
        return list(new LambdaQueryWrapper<HrmEFolder>()
        .eq(HrmEFolder::getDataStatus,1)
        .eq(HrmEFolder::getParentId,id));
    }

    public void getFolder(List<HrmEFolder> hrmEFolders,List<HrmEFolderResult> hrmEFolderResults,Map<Long, List<HrmEFolder>> map){

        for (HrmEFolder hrmEFolder : hrmEFolders) {
            HrmEFolderResult hrmEFolderResult = new HrmEFolderResult();
            BeanUtil.copyProperties(hrmEFolder,hrmEFolderResult);
            hrmEFolderResults.add(hrmEFolderResult);
            List<HrmEFolder> childList = map.get(hrmEFolder.getId());
            if(CollUtil.isNotEmpty(childList)){
                List<HrmEFolderResult> childFolderList = new ArrayList<>();
                getFolder(childList,childFolderList,map);
                hrmEFolderResult.setChildren(childFolderList);
            }else{
                List<HrmEFolderResult> childFolderList = new ArrayList<>();
                hrmEFolderResult.setChildren(childFolderList);
            }

        }
    }
}
